package cool.mtc.minghe.web.controller.sys.dict;

import cool.mtc.core.page.PageData;
import cool.mtc.minghe.data.model.base.Dict;
import cool.mtc.minghe.data.model.base.IdForm;
import cool.mtc.minghe.data.model.sys.dict.dto.DictDTO;
import cool.mtc.minghe.data.model.sys.dict.dto.DictListDTO;
import cool.mtc.minghe.data.model.sys.dict.form.DictForm;
import cool.mtc.minghe.data.model.sys.dict.query.DictQuery;
import cool.mtc.minghe.service.service.sys.dict.DictService;
import cool.mtc.minghe.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 明河
 */
@Tag(name = "字典管理")
@RestController
@RequestMapping("api/sys/dict")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictController extends BaseController {
    private final DictService dictService;

    @PreAuthorize("@ps.hasPermission('sys:dict:insert')")
    @Tag(name = "字典管理")
    @Operation(summary = "新增", description = "权限：sys:dict:insert")
    @PostMapping("insert")
    public long insert(@RequestBody @Validated DictForm form) {
        return dictService.insert(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict:update')")
    @Tag(name = "字典管理")
    @Operation(summary = "更新", description = "权限：sys:dict:update")
    @PostMapping("update")
    public void update(@RequestBody @Validated DictForm form) {
        dictService.update(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict:delete')")
    @Tag(name = "字典管理")
    @Operation(summary = "删除", description = "权限：sys:dict:delete")
    @PostMapping("delete")
    public void delete(@RequestBody IdForm form) {
        dictService.delete(form.getId());
    }

    @PreAuthorize("@ps.hasPermission('sys:dict:read')")
    @Tag(name = "字典管理")
    @Operation(summary = "分页列表", description = "权限：sys:dict:read")
    @GetMapping("list/page")
    public PageData<DictListDTO> pageList(DictQuery query) {
        return dictService.pageList(query);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict:read')")
    @Tag(name = "字典管理")
    @Operation(summary = "详情", description = "权限：sys:dict:read")
    @GetMapping("info")
    public DictDTO info(Long id) {
        return dictService.info(id);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict:dict')")
    @Tag(name = "字典管理")
    @Operation(summary = "字典（根据字典类型编码获取）", description = "权限：sys:dict:dict")
    @GetMapping("list/dict/by_type_codes")
    public Map<String, List<Dict>> dictListByDictTypeCodes(@RequestParam List<String> dictTypeCodes) {
        return dictService.dictListByDictTypeCodes(dictTypeCodes);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict:dict')")
    @Tag(name = "字典管理")
    @Operation(summary = "字典（根据字典类型编码获取）", description = "权限：sys:dict:dict")
    @GetMapping("list/dict/by_type_code")
    public List<Dict> dictListByDictTypeCode(@RequestParam String type) {
        return dictService.dictListByDictTypeCode(type);
    }

    @PreAuthorize("@ps.hasPermission('sys:dict:dict')")
    @Tag(name = "字典管理")
    @Operation(summary = "字典（根据字典类型ID获取）", description = "权限：sys:dict:dict")
    @GetMapping("list/dict/by_type_ids")
    public Map<String, List<Dict>> dictListByDictTypeIds(@RequestParam List<String> dictTypeIds) {
        return dictService.dictListByDictTypeIds(dictTypeIds);
    }
}
